package org.robot.work.into.oalib;

import java.io.File;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.robot.Spider;
import org.robot.component.Attachment;
import org.robot.component.Page;
import org.robot.dao.IssueDao;
import org.robot.dao.JournalDao;
import org.robot.dao.PaperDao;
import org.robot.pojo.Issue;
import org.robot.pojo.Journal;
import org.robot.pojo.Paper;
import org.robot.utils.RegexUtil;
import org.robot.utils.XmlUtil;

/**
 * 实验力学
 * @author pdz
 * @date 2019年1月23日
 */
public class Sylx extends Spider {
	private static final Long TASKID = 1700L;
	private static List<Attachment> list = Collections.synchronizedList(new LinkedList<>());

	@Override
	public void parse(Page page) throws Exception {
		if (page.isSeed()) {
			parseJournal(page);
			IntStream.rangeClosed(2015, 2018).forEach(year -> 
			IntStream.rangeClosed(1, 6).forEach(id -> page.addNext("http://sylx.ustc.edu.cn/sylx/ch/reader/issue_list.aspx?year_id="+year+"&quarter_id="+id, "issue")));
		} else if (page.typeEquals("issue")) {
			parseIssue(page);
		} else if (page.typeEquals("paper")) {
			parsePaper(page);
		}
		
	}
	
	private void parsePaper(Page page) {
		Long issueid = IssueDao.selectPidByWebsite(Issue.class, page.prevUrl(), TASKID);
		String refText = page.select("span#ReferenceText").text();
		String pages = RegexUtil.regexFirst("\\d+~\\d+", refText);
		String title = joinText(page, "#FileTitle, #EnTitle");
		String doi = page.select("span#DOI a").text();
		String keywords = joinText(page, "span#KeyWord, span#EnKeyWord");
		List<String> authorList = page.select("span#Author table tr td:eq(0) a").eachText().stream().map(aut -> aut.replaceAll("\\*|\\d", "")).collect(Collectors.toList());
		String authors = String.join(",", authorList);
		List<String> affList = page.select("span#Author table tr td:eq(1) a").eachText();
		String affs = String.join(",", affList);
		String abst = joinText(page, "span#Abstract, span#EnAbstract");
		String fileUrl = page.select("span#URL a").first().absUrl("href");
		
		String[] pageArr = pages.split("~");
		page.put("title", title);
		page.put("authorName", authors);
		page.put("keywords", keywords);
		page.put("paperAbstract", abst);
		page.put("fileName", "");
		page.put("fileUrl", fileUrl);
		page.put("imageFileName", "");
		page.put("doi", doi);
		page.put("affs", affs);
		page.put("startPage", pageArr[0]);
		page.put("endPage", pageArr[1]);
		list.add(page.getAttachment());
		
		Paper paper = new Paper();
		paper.setWebsite(page.url());
		paper.setTitle(title);
		paper.setTaskid(TASKID);
		paper.setDoi(doi);
		paper.setAbstracts(abst);
		paper.setPdfUrl(fileUrl);
		paper.setAuthors(authors);
		paper.setAffilications(affs);
		paper.setKeywords(keywords);
		paper.setPage(pages);
		paper.setIssueId(issueid);
		PaperDao.addPaper(paper);
	}


	private String joinText(Page page, String selector) {
		List<String> list = page.select(selector).eachText();
		return String.join("<br>", list);
	}

	private void parseIssue(Page page) {
		String url = page.url();
		String pubYear = RegexUtil.getYear(url);
		String quarter = url.substring(url.lastIndexOf("=")+1);
		String issueName = pubYear + "年第" + quarter + "期";
		page.put("pubYear", pubYear);
		page.put("issueName", issueName);
		
		Long journalid = JournalDao.selectPidByWebsite(Journal.class, page.prevUrl(), TASKID);
		Issue issue = new Issue();
		issue.setIssue(issueName);
		issue.setYear(pubYear);
		issue.setTaskId(TASKID);
		issue.setWebsite(page.url());
		issue.setJournalId(journalid);
		IssueDao.addIssue(issue);
	}

	private void parseJournal(Page page) {
		page.put("fullEnglishName", "Journal of Experimental Mechanics");
		page.put("fullChineseName", "实验力学");
		page.put("shortEnglishName", "");
		page.put("issnPrint", "1001-4888");
		page.put("issnOnline", "");
		
		Journal journal = new Journal();
		journal.setWebsite(page.url());
		journal.setIssn("1001-4888");
		journal.setTaskId(TASKID);
		journal.setJournalname("实验力学<br>Journal of Experimental Mechanics");
		JournalDao.addJournal(journal);
	}

	public static void main(String[] args) {
		Sylx claw = new Sylx();
		//http://sylx.ustc.edu.cn/sylx/ch/reader/issue_list.aspx?year_id=2016&quarter_id=3
		claw.setBaseDirectory("E:/webSpider");
		claw.addSeed("http://sylx.ustc.edu.cn/sylx/ch/index.aspx");
		claw.addByHand();
		claw.addRegex("http://sylx.ustc.edu.cn/sylx/ch/reader/view_abstract.aspx.file_no=\\d+.flag=\\d+", "paper");
		claw.start();
		
		claw.asXml();
		
	}


	private void asXml() {
		for (int i = 0; i < list.size(); i+=50) {
			int toIndex = i + 50 > list.size() ? list.size() : i + 50;
			List<Attachment> subList = list.subList(i, toIndex);
			XmlUtil.generateXml(subList, new File(this.getConfig().getBaseDirectory()+"/xml", getClass().getSimpleName()+i+".xml"));
		}
	}

}
